/**
 * paginationRequest
 *
 * url = srvMap.get('xxx')
 * initParam
 * hasNextFunction(lastParam, data, flag) return true/false
 * nextParamFunction(lastParam, data, flag) return nextParam
 * firstRequestCallback(lastParam, data, flag) bind html
 * moreRequestCallback(lastParam, data, flag) bind html
 */
 
 (function($){
	$.extend($, {
		paginationRequest : (function(){
			var hasNext;
			var nextParam;
			var scrollBottomFunction;
			var showLoding;
			var isLastPage;
			return function(url, initParam, hasNextFunction, nextParamFunction, firstRequestCallback, moreRequestCallback){
				var searchRequest = function(url, param, callback){
					//var loadingEle = "<div id='loading2-box' style='z-index:9999999;position: fixed;text-align: center;bottom: 55px;width: 100%;'><img src='../../assets/img/loading2.gif' style='width: 20px;'></div>";

					Util.ajax.postJson(url, param, function(data, flag){
						var hasNext = hasNextFunction(param, data, flag);
						//alert("hasNext"+hasNext);
						if(!hasNext){
							$("#loading2-box").hide();
							$(".footer-full").show();
						}
						else{
							$("#loading2-box").show();
							$(".footer-full").hide();
						}
						nextParam = nextParamFunction(param, data, flag);
						callback && callback(param, data, flag);
					});
				}
				//µÚÒ»´ÎÇëÇó
				searchRequest(url, initParam, function(param, data, flag){
					firstRequestCallback && firstRequestCallback(param, data, flag);
					//如果第一次只有一页，直接显示底部logo
					if(data.bean.total === data.beans.length){
						$("#loading2-box").hide();
						$(".footer-full").show();
						//isLastPage = true;
					}
					if(scrollBottomFunction){
						$(window).unbind('scroll',scrollBottomFunction);
						// document.removeEventListener("touchmove",hideLoading);
					}
					scrollBottomFunction = function () {
						if ($(document).scrollTop() + $(window).height() >= $(document).height()) {
							if(hasNext){
								//¼ÓÔØ¸ü¶à
								searchRequest(url, nextParam, function(param, data, flag){

									moreRequestCallback && moreRequestCallback(param, data, flag);
								});
							}else{
								//isLastPage = true;
								$("#loading2-box").hide();
								$(".footer-full").show();
								// Util.Tips.warning("没有更多商品了", 'bottom', true);
							}
						}
					}
					showLoding = function(){
					if(!hasNext){
						$("#loading2-box").hide();
						$(".footer-full").show();
						}
						else{
							$("#loading2-box").show();
							$(".footer-full").hide();
						}
					};
					document.addEventListener("touchmove",showLoding,false);
					$(window).scroll(scrollBottomFunction);
				});
			};
		})()
	});
})(jQuery);